package com.csx.base.concurrency;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p> what does this class do ? </p>
 * <p> How does it work ?</p>
 * <p> Why does it do this ?</p>
 *
 * @author cuisongxu
 * @date 2024/11/24 周日 16:29
 */
public class TestReetrantLockWait implements Runnable{

    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();
    public static void main(String[] args) throws InterruptedException {
        TestReetrantLockWait testReetrantLockWait = new TestReetrantLockWait();
        Thread thread = new Thread(testReetrantLockWait);
        thread.start();
        TimeUnit.SECONDS.sleep(5);
        lock.lock();
        try{
            System.out.println("signal 通知线程继续执行");
            condition.signal();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    @Override
    public void run() {
        lock.lock();
        try {
            System.out.println("await等待条件满足");
            condition.await();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
